<?php
namespace app\api\task;




use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Log;
use Workerman\Mqtt\Client;
use Workerman\Worker;

class Mqtt extends Command
{

    public  $mqtt;

    protected function configure()
    {
        $this->setName('mqtt')
            ->setDescription('mqtt');
    }

    protected function execute(Input $input, Output $output){
        $this->subscribe();
    }
    protected function subscribe(){



        $worker = new Worker();
        $worker->onWorkerStart = function(){
            $option =array();
            $username = 'lvzaina';
            $password = "iot@auth.lvzaina";
            $option['clean_session'] = 1;
            $option['username'] = $username;
            $option['password'] = $password;

            $mqtt = new Client('mqtt://47.97.67.38:1883',$option);
            $mqtt->onConnect = function($mqtt) {
                $mqtt->subscribe('CIOT/+/DEVICE/+');
            };
            $mqtt->onMessage = function($topic, $content){
                $s = json_encode($topic)."=========mqtt===========".json_encode($content);

                Log::info("=wxf==mqtt=subscribe=====".$s);
                error_log("===wxf====mqtt=subscribe====2".$s);

            };
            $mqtt->connect();
        };
        Worker::runAll();
        Log::info("=wxf==mqtt=start=".date("Y-m-d H:i:s"));

    }

}